using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The ReligionCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about ReligionCodeTable objects.
/// </summary>
public class ReligionCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of ReligionCodeTable from the underlying datasource.
/// </summary>
/// <param name="religionCode">The unique ReligionCode of the ReligionCodeTable in the database.</param>
/// <returns>An ReligionCodeTable when the ReligionCode was found in the database, or null otherwise.</returns>
public static ReligionCodeTable GetItem(short religionCode)
{
ReligionCodeTable myReligionCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocReligionCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@religionCode", religionCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myReligionCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myReligionCodeTable;
}
}

/// <summary>
/// Returns a list with ReligionCodeTable objects.
/// </summary>
/// <returns>A generics List with the ReligionCodeTable objects.</returns>
public static ReligionCodeTableList GetList()
{
ReligionCodeTableList tempList = new ReligionCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocReligionCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a ReligionCodeTable in the database.
/// </summary>
/// <param name="myReligionCodeTable">The ReligionCodeTable instance to save.</param>
/// <returns>The new ReligionCode if the ReligionCodeTable is new in the database or the existing ReligionCode when an item was updated.</returns>
public static Int16 Save(ReligionCodeTable myReligionCodeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocReligionCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myReligionCodeTable.ReligionCode == -1)
{myCommand.Parameters.AddWithValue("@religionCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@religionCode", myReligionCodeTable.ReligionCode);
}
if (string.IsNullOrEmpty(myReligionCodeTable.Description))
{
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myReligionCodeTable.Description);
}
if (myReligionCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myReligionCodeTable.Status);
}
if (myReligionCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myReligionCodeTable.CreatedDateTime);
}
if (myReligionCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myReligionCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a ReligionCodeTable from the database.
/// </summary>
/// <param name="religionCode">The ReligionCode of the ReligionCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(short religionCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocReligionCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@religionCode", religionCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the ReligionCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static ReligionCodeTable FillDataRecord(IDataRecord myDataRecord )
{
ReligionCodeTable myReligionCodeTable = new ReligionCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ReligionCode")))
{
myReligionCodeTable.ReligionCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("ReligionCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myReligionCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myReligionCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myReligionCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myReligionCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myReligionCodeTable;
}
}

 } 
